package com.example.mobilesafe.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.mobilesafe.R;
import com.example.mobilesafe.constant.ConstantValue;
import com.example.mobilesafe.utils.SharedPreferencesUtil;


public class ToastLocationActivity extends Activity {

    private ImageView iv_drag;

    private Button btn_top;

    private Button btn_bottom;

    private int startX;

    private int startY;

    private WindowManager mWindowManager;

    private int mScreenHeight;

    private int mScreenWidth;

    // 存储点击事件的数组，数组容量就是点击次数
    private long[] mHits = new long[2];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast_location);

        // 初始化UI
        initUI();
    }

    /**
     * 初始化UI
     */
    private void initUI() {
        iv_drag = findViewById(R.id.iv_drag);
        btn_top = findViewById(R.id.btn_top);
        btn_bottom = findViewById(R.id.btn_bottom);

        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mScreenHeight = mWindowManager.getDefaultDisplay().getHeight();
        mScreenWidth = mWindowManager.getDefaultDisplay().getWidth();

        // 从sp中读取的坐标值
        int locationX = SharedPreferencesUtil.getInt(getApplicationContext(), ConstantValue.LOCATION_X, 0);
        int locationY = SharedPreferencesUtil.getInt(getApplicationContext(), ConstantValue.LOCATION_Y, 0);

        // 左上角的坐标作用在iv_drag上，由于该控件在相对布局中，所以其所在位置的规则需要由相对布局提供
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); // 指定宽高
        layoutParams.leftMargin = locationX; // 配置左上角的X坐标
        layoutParams.topMargin = locationY; // 配置左上角的Y坐标

        // 将以上规则作用在iv_drag上
        iv_drag.setLayoutParams(layoutParams);

        if (locationY > mScreenHeight / 2){ // 移动到屏幕的下半部分
            btn_bottom.setVisibility(View.INVISIBLE);
            btn_top.setVisibility(View.VISIBLE);
        }else { // 移动到屏幕的上半部分
            btn_bottom.setVisibility(View.VISIBLE);
            btn_top.setVisibility(View.INVISIBLE);
        }

        // 监听某一个控件的双击事件
        iv_drag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.arraycopy(mHits,1,mHits,0,mHits.length - 1);
                mHits[mHits.length - 1] = SystemClock.uptimeMillis();
                if (mHits[mHits.length - 1] - mHits[0] < 500){ // 两次点击间隔0.5秒
                    // 满足双击事件后的调用
                    int left = mScreenWidth / 2 - iv_drag.getWidth() / 2;
                    int top = mScreenHeight / 2 - iv_drag.getHeight() / 2;
                    int right = mScreenWidth / 2 + iv_drag.getWidth() / 2;
                    int bottom = mScreenHeight / 2 + iv_drag.getHeight() / 2;

                    // 控件按以上提供的四个坐标来显示
                    iv_drag.layout(left,top,right,bottom);

                    // 存储最终位置
                    SharedPreferencesUtil.putInt(getApplicationContext(), ConstantValue.LOCATION_X,iv_drag.getLeft());
                    SharedPreferencesUtil.putInt(getApplicationContext(), ConstantValue.LOCATION_Y,iv_drag.getTop());
                }
            }
        });

        // 监听某一个控件的拖拽过程（按下（1）、移动（多次）、抬起（1））
        iv_drag.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int moveX = (int) event.getRawX();
                        int moveY = (int) event.getRawY();
                        int disX = moveX - startX;
                        int disY = moveY - startY;

                        // 1.获取当前控件所在屏幕的（左,上）角的位置
                        int left = iv_drag.getLeft() + disX; // 左侧坐标
                        int top = iv_drag.getTop() + disY; // 上侧坐标
                        int right = iv_drag.getRight() + disX; // 右侧坐标
                        int bottom = iv_drag.getBottom() + disY; // 下侧坐标

                        // 容错处理（iv_drag不能拖拽出手机屏幕）
                        if (left < 0){ // 左边缘不能超出屏幕
                            return true;
                        }
                        if (right > mScreenWidth){ // 右边缘不能超出屏幕
                            return true;
                        }
                        if (top < 0){ // 上边缘不能超出屏幕
                            return true;
                        }
                        if (bottom > mScreenHeight - 22){ // 下边缘（屏幕的高度 - 22 = 底边缘的显示最大值）不能超出屏幕
                            return true;
                        }

                        if (top > mScreenHeight / 2){ // 移动到屏幕的下半部分
                            btn_bottom.setVisibility(View.INVISIBLE);
                            btn_top.setVisibility(View.VISIBLE);
                        }else { // 移动到屏幕的上半部分
                            btn_bottom.setVisibility(View.VISIBLE);
                            btn_top.setVisibility(View.INVISIBLE);
                        }

                        // 2.告知移动的控件，根据计算出来的坐标去做展示
                        iv_drag.layout(left,top,right,bottom);

                        // 3.重置一次起始坐标
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_UP:
                        // 4.记录上次移动后的坐标位置
                        SharedPreferencesUtil.putInt(getApplicationContext(), ConstantValue.LOCATION_X,iv_drag.getLeft());
                        SharedPreferencesUtil.putInt(getApplicationContext(), ConstantValue.LOCATION_Y,iv_drag.getTop());
                        break;
                }
                // 在当前的情况下返回false表示不响应事件，返回true才表示响应事件
                // 既要响应点击事件，又要响应拖拽过程，则此返回值结果需要修改为false
                return false;
            }
        });
    }
}